package onlineclass.servlet;

import onlineclass.controller.OMajorController;
import onlineclass.controller.OResourceController;
import onlineclass.controller.OUserController;
import onlineclass.controller.OVideoController;
import onlineclass.util.StringUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

@WebServlet("*.do")
public class MainServlet extends HttpServlet {
    private OUserController ouser=new OUserController();
    private OMajorController omajor=new OMajorController();
    private OResourceController oresource=new OResourceController();
    private OVideoController ovideo=new OVideoController();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        path = path.substring(1, path.length() - 3);
        //path其实就是方法名，参数是可预期的
        String ar[] = path.split("-");
        if (ar.length == 2) {
            try {
                String field = ar[0];
                String methodName = ar[1];
                Field f = this.getClass().getDeclaredField(field);
                f.setAccessible(true);
                Object controller = f.get(this);//??? main.subject   main.grade
                Method m2 = controller.getClass().getDeclaredMethod(methodName, HttpServletResponse.class, HttpServletRequest.class);
                Object rtn = m2.invoke(controller, response, request);

                if (rtn instanceof String) {
                    if (StringUtil.isBlank(rtn))
                        return;
                    String location = (String) rtn;
                    if (location.startsWith("redirect:")) {
                        response.sendRedirect(location.substring(9));
                        return;
                    }
                    request.getRequestDispatcher((String) rtn).forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
                response.sendError(501, "方法没有找到！");
            }
        } else {
            try {
                Method method = this.getClass().getDeclaredMethod(path, HttpServletResponse.class, HttpServletRequest.class);
                Object rtn = method.invoke(this, response, request);
                if (rtn instanceof String) {
                    if (StringUtil.isBlank(rtn))
                        return;
                    String location = (String) rtn;
                    if (location.startsWith("redirect:")) {
                        response.sendRedirect(location.substring(9));
                        return;
                    }
                    request.getRequestDispatcher((String) rtn).forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
                response.sendError(501, "方法没有找到！");
            }
        }
    }
}
